package com.deyuanyun.pic.settings.service.prvlg.impl;

import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.dto.prvlg.ModifyState;
import com.deyuanyun.pic.settings.controller.dto.prvlg.RoleGrant;
import com.deyuanyun.pic.settings.controller.dto.prvlg.RoleQuery;
import com.deyuanyun.pic.settings.domain.prvlg.Menu;
import com.deyuanyun.pic.settings.domain.prvlg.Role;
import com.deyuanyun.pic.settings.domain.prvlg.RoleExample;
import com.deyuanyun.pic.settings.domain.prvlg.RoleMenuR;
import com.deyuanyun.pic.settings.mapper.prvlg.MenuMapper;
import com.deyuanyun.pic.settings.mapper.prvlg.RoleMapper;
import com.deyuanyun.pic.settings.mapper.prvlg.RoleMenuRMapper;
import com.deyuanyun.pic.settings.mapper.prvlg.dao.RoleDao;
import com.deyuanyun.pic.settings.service.prvlg.RoleMgrService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 权限模块 “角色”服务接口实现类
 *
 * 方法详情：<br/>
 *
 * @author huanglw
 * @date  2016-11-28
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class RoleMgrServiceImpl implements RoleMgrService {

	@Resource
	private RoleMapper roleMapper;
	@Resource
	private RoleDao roleDao;
	@Resource
	private RoleMenuRMapper roleMenuRMapper;
	@Resource
	private MenuMapper menuMapper;

	@Override
	public List<Role> query(RoleQuery roleQuery) {
		RoleExample example = new RoleExample();
		RoleExample.Criteria criteria = example.createCriteria().andDeletedEqualTo((short) 0);
		if (StringUtils.isNotNullEmpty(roleQuery.getCode())){
			criteria.andCodeEqualTo(roleQuery.getCode());
		}
		if (StringUtils.isNotNullEmpty(roleQuery.getName())){
			criteria.andNameLike(roleQuery.getName());
		}
		return roleMapper.selectByExample(example);
	}

	@Override
	public Role find(String id) {
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public int edit(Role role) {
		if (StringUtils.isNotNullEmpty(role.getId())){
			Role roleObj = this.find(role.getId());
			Asserts.assertLogic(roleObj == null, "角色 不存在");
			Asserts.assertLogic(roleObj.getState() != null && roleObj.getState() == 0, "该 角色 已启用,不能“编辑”");
			return roleMapper.updateByPrimaryKeySelective(role);
		}
		role.setId(CommonUtil.getUUID());
		role.setState((short)1);
		return roleMapper.insertSelective(role);
	}

	@Override
	public int modifyState(ModifyState roles) {
		int count = 0;
		for (String id : roles.getIds()){
			Role role = new Role();
			role.setId(id);
			role.setState(roles.getState());
			roleMapper.updateByPrimaryKeySelective(role);
			count++;
		}
		return count;
	}

	@Override
	public int delete(String[] ids) {
		int count = 0;
		for (String id : ids) {
			Role role = this.find(id);
			Asserts.assertLogic(role == null, "角色 不存在");
			Asserts.assertLogic(role.getState() != null && role.getState() == 0, "该 角色 已启用,不能“删除”");
			role = new Role();
			role.setId(id);
			role.setDeleted(Short.valueOf("1"));
			role.setUpdateDate(new Date());
	//		role.setUpdateUser(realName);
			role.setUpdateRemark("删除");
			this.edit(role);
			count++;
		}
		return count;
	}

	@Override
	public List<Menu> queryMenu(String roleId) {
		return menuMapper.queryMenuAll(roleId);
	}

	@Override
	public int grant(RoleGrant roleGrant) {

		int count = 0;
		roleDao.deleteAuthorize(roleGrant.getId());
		for (String menuId : roleGrant.getMenuIds()){
			RoleMenuR roleMenuR = new RoleMenuR();
			roleMenuR.setId(CommonUtil.getUUID());
			roleMenuR.setRoleId(roleGrant.getId());
			roleMenuR.setMenuId(menuId);
			roleMenuRMapper.insertSelective(roleMenuR);
			count++;
		}
		return count;
	}

}
